'use strict';

const {db_dialect, db_username, db_password, db_host, db_port, db_connetcion_database} = require('../config/db.js');
const { Sequelize, Op } = require('sequelize');
const sequelize = new Sequelize(`${db_dialect}://${db_username}:${db_password}@${db_host}:${db_port}/${db_connetcion_database}`);
const models = require('../models');

let result = {
    Op,
    db: sequelize,
    table:{},
},
table = {};

(async () => {
    for (const tableName in models) {
        if (models.hasOwnProperty(tableName)) {
            const model = models[tableName];
            let sequelizeModel = sequelize.define(tableName.toLocaleLowerCase(), model);
            table[tableName] = sequelizeModel;
            await sequelizeModel.sync({force:true}).then(() => {
                sequelizeModel.bulkCreate([
                    {
                        product_name: 'OL装 上装',
                        price: 588,
                        stock: 10,
                        supplier: 'UR'
                    },
                    {
                        product_name: 'OL装 下装',
                        price: 699,
                        stock: 5,
                        supplier: 'UR'
                    },
                    {
                        product_name: '泳装',
                        price: 299,
                        stock: 100,
                        supplier: '维多利亚的秘密'
                    },
                    {
                        product_name: '唐装',
                        price: 1999,
                        stock: 2,
                        supplier: '曹县'
                    },
                    {
                        product_name: '童装',
                        price: 199,
                        stock: 50,
                        supplier: '好孩子'
                    }
                ]);
            });
        }
    }
})()

result.table = table;
module.exports = result;